<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<script type="text/javascript"> 
<!--
   window.onload=function(){
       var test="<p><b>this</b> is a <a href='#'>demo</a>!<img src='test.jpg' /></p>";
       $("but1").onclick=function(){ 
			alert(strip_tags(test,"")); 
		}
	   $("but2").onclick=function(){ 
			alert(strip_tags(test,"<b>")); 
	    }
       $("but3").onclick=function(){ 
		    alert(strip_tags(test,"<img>")); 
	   }
	   $("but4").onclick=function(){ 
		    alert(strip_tags(test,"<p><a>")); 
	   }
   }
   function $(oid){ return document.getElementById(oid); }
   //stripStr是要除去标签的字符串，allowTags是可保留的标签
   function strip_tags(stripStr,allowTags){
       var allowArr=[];
	   if(!stripStr || stripStr.length==0) return stripStr;
	   if (allowTags) allowArr=allowTags.match(/[a-zA-Z]+/gi);
	   var matchArr = stripStr.match(/(<\/?[\S][^>]*>)/gi);
	   for (var mVal in matchArr){
		   if (isNaN(mVal)) continue; 
		   var matchVal = matchArr[mVal];
		   var allow = false;
		   for (var aVal in allowArr){
			   if(new RegExp('<\\/?'+allowArr[aVal]+'(?:>| )','i').test(matchVal)){
				   allow=true
				   break;
			   }               
		   }
		   if (!allow) stripStr=stripStr.split(matchVal).join("");
	   }
	   return stripStr;
   }
--> 
</script> 
</head> 
<body> 
   <h1>JavaScript版strip_tags函数Demo</h1> 
   <p>变量test的值为:   &lt;p&gt;&lt;b&gt;this&lt;/b&gt; is a &lt;a href='#'&gt;demo&lt;/a&gt;!&lt;img src='test.jpg' /&gt;&lt;/p&gt;</p> 
   <p><button id="but1">单击执行alert(strip_tags(test,""))</button></p> 
   <p><button id="but2">单击执行alert(strip_tags(test,"&lt;b&gt;"))</button></p> 
   <p><button id="but3">单击执行alert(strip_tags(test,"&lt;img&gt;"))</button></p> 
   <p><button id="but4">单击执行alert(strip_tags(test,"&lt;p&gt;&lt;a&gt;"))</button></p> 
</body> 
</html>